/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fiu.accesslog.statistics;

import edu.fiu.accesslog.SingleAccessLog;
import java.util.ArrayList;
import java.util.HashMap;
import nl.bitwalker.useragentutils.Browser;
import nl.bitwalker.useragentutils.OperatingSystem;
import nl.bitwalker.useragentutils.UserAgent;

/**
 *
 * @author Hasan Mahmud @date Jun 18, 2012
 */
public class UserAgentStatistics
{

        private HashMap<String, Info> browserStatistics;
        private HashMap<String, Info> browserTypeStatistics;
        private HashMap<String, Info> browserRenderingEngineStatistics;
        private HashMap<String, Info> browserManufacturerStatistics;
        private HashMap<String, Info> deviceTypeStatistics;
        private HashMap<String, Info> osStatistics;
        private HashMap<String, Info> osManufacturerStatistics;
        
        
        private static UserAgentStatistics singletonInstance = null;
        
        private Browser browser;
        private OperatingSystem os;

        private UserAgentStatistics()
        {
                browserTypeStatistics = new HashMap<String, Info>(30);
                browserStatistics = new HashMap<String, Info>(30);

                browserRenderingEngineStatistics = new HashMap<String, Info>(30);
                browserManufacturerStatistics = new HashMap<String, Info>(30);

                deviceTypeStatistics = new HashMap<String, Info>(30);


                osStatistics = new HashMap<String, Info>(30);
                osManufacturerStatistics = new HashMap<String, Info>(30);
        }

        public static synchronized UserAgentStatistics getInstance()
        {
                if (singletonInstance == null)
                {
                        singletonInstance = new UserAgentStatistics();
                }

                return singletonInstance;
        }

        public synchronized void addAccessLogEntry(SingleAccessLog saLog, boolean isUnique)
        {

                try
                {


                        
                        browser = saLog.getUserAgent().getBrowser();
                        os = saLog.getUserAgent().getOperatingSystem();


                        RecordUtility.addInHashMap(browserStatistics, browser.getName(), isUnique);
                        RecordUtility.addInHashMap(browserManufacturerStatistics, browser.getManufacturer().getName(), isUnique);
                        
                        RecordUtility.addInHashMap(browserTypeStatistics, browser.getBrowserType().getName(), isUnique);
                        RecordUtility.addInHashMap(browserRenderingEngineStatistics, browser.getRenderingEngine().toString(), isUnique);

                        RecordUtility.addInHashMap(deviceTypeStatistics, os.getDeviceType().name(), isUnique);

                        RecordUtility.addInHashMap(osStatistics, os.getName(), isUnique);
                        RecordUtility.addInHashMap(osManufacturerStatistics, os.getManufacturer().getName(), isUnique);



                }
                catch (NullPointerException ex)
                {
                }
        }

        public ArrayList<Info> getBrowserStatistics()
        {
                return RecordUtility.getClonedArrayList(browserStatistics);
        }

        public ArrayList<Info> getBrowserTypeStatistics()
        {
                return RecordUtility.getClonedArrayList(browserTypeStatistics);
        }

        public ArrayList<Info> getbrowserRenderingEngineStatistics()
        {
                return RecordUtility.getClonedArrayList(browserRenderingEngineStatistics);
        }

        public ArrayList<Info> getBrowserManufacturerStatistics()
        {
                return RecordUtility.getClonedArrayList(browserManufacturerStatistics);
        }

        public ArrayList<Info> getDeviceTypeStatistics()
        {
                return RecordUtility.getClonedArrayList(deviceTypeStatistics);
        }

        public ArrayList<Info> getOSStatistics()
        {
                return RecordUtility.getClonedArrayList(osStatistics);
        }

        public ArrayList<Info> getOSManufacturerStatistics()
        {
                return RecordUtility.getClonedArrayList(osManufacturerStatistics);
        }
}
